package com.zy.fee.roadpark.util;

import static com.printer.sdk.PrinterConstants.BarcodeType.DATAMATRIX;
import static com.printer.sdk.PrinterConstants.BarcodeType.PDF417;

import com.printer.sdk.Barcode;
import com.printer.sdk.PrinterConstants;
import com.spd.print.jx.impl.PrintImpl;
import com.zy.fee.roadpark.app.AppApplication;
import com.zy.fee.roadpark.dao.CommonLogic;
import com.zy.fee.roadpark.model.apimodel.APIM_LotInfo;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.PreferencesHelper;

/**
 * Created by djy 2023/7/12 0012 上午 10:47
 */

public class Printer {
   public static void printT(String plateNumber, String printTime, String url, Boolean isNp, String lotID) {
      CommonLogic.lotInfo(AppApplication.getContext(), lotID, new HttpRequest.HttpCallback() {
         @Override
         public void onStart() {
         }

         @Override
         public void onSucceed(Object obj) {
            APIM_LotInfo result = (APIM_LotInfo) obj;
            printTicket(plateNumber, printTime, url, isNp, result.getLotInfo().getOverMin());
         }

         @Override
         public void onFailed() {
         }

         @Override
         public void onFinish() {
         }
      });
   }

   private static void printTicket(String plateNumber, String printTime, String url, Boolean isNp, int min) {
      PreferencesHelper preferencesHelper = new PreferencesHelper(AppApplication.getContext());
      PrintImpl mPrinter = AppApplication.getPrinterImpl();
      mPrinter.initPrinter();
      mPrinter.setPrinter(2, PrinterConstants.Command.ALIGN_CENTER);
      mPrinter.setFont(0, 1, 1, 0, 0);
      mPrinter.printText("临停缴费信息单");
      mPrinter.setPrinter(1, 1);

      StringBuilder sb = new StringBuilder();
      sb.append("车牌号:      " + plateNumber);
      sb.append("\n");
      sb.append("所在停车场:  " + preferencesHelper.getSettingString(PreferencesHelper.User.Key.LOT_NAME));
      sb.append("\n");
      sb.append("取证人编码:  " + preferencesHelper.getSettingString(PreferencesHelper.User.Key.USER_NAME));
      sb.append("\n");
      sb.append("起停时间:    " + printTime);
      sb.append("\n");
      sb.append("收款单位:    " + "尚盈车联");
      sb.append("\n");
      sb.append("客服电话:    " + "4009988406");
      sb.append("\n");
      sb.append("\n");

      sb.append("此路段全程电子监控，请自觉缴费！");
      sb.append("\n");
      sb.append("恶意逃费行为将计入好享泊个人诚信系统，后期对您的停车权益产生影响");
      sb.append("\n");
      if(isNp) {
         sb.append("\n");
         sb.append("如需驶离，请扫描二维码进入小程序订单详情操作申请驶离，收费员会进行处理");
         sb.append("\n");
      }

      // 字号使用默认
      mPrinter.setFont(0, 0, 0, 0, 0);
      mPrinter.setPrinter(2, 0);
      // 打印
      mPrinter.printText(sb.toString());

      // 二维码图片
      Barcode barcode;
      int[] width = new int[] { 0, 1, 0 };
      int[] height = new int[] { 76, 0, 8 };
      byte[] bytes = new byte[] { PrinterConstants.BarcodeType.QRCODE, PDF417, DATAMATRIX };
      barcode = new Barcode(bytes[0], width[0], height[0], 6, url);
      mPrinter.setPrinter(2, PrinterConstants.Command.ALIGN_CENTER);
      mPrinter.printBarCode(barcode);
      mPrinter.setPrinter(1, 1);

      sb = new StringBuilder();
      sb.append("微信或支付宝扫码自助缴费");
      sb.append("\n");
      mPrinter.setPrinter(2, PrinterConstants.Command.ALIGN_CENTER);
      mPrinter.setFont(0, 0, 0, 0, 0);
      mPrinter.printText(sb.toString());

      sb = new StringBuilder();
      sb.append("\n");
      sb.append("付费完成请在" + min + "分钟内驶离，否则重新计费！");
      sb.append("\n");
      sb.append("温馨提示：本停车位收取的费用为车位使用费,");
      sb.append("不含车辆保管费用，勿将贵重物品遗留在车上，");
      sb.append("请随身携带，谨防被盗，谢谢合作！");
      sb.append("\n");
      sb.append("\n");
      sb.append("\n");
      sb.append("\n");
      sb.append("\n");
      sb.append("\n");
      sb.append("\n");
      // 字号使用默认
      mPrinter.setFont(0, 0, 0, 0, 0);
      mPrinter.setPrinter(2, 0);
      // 打印
      mPrinter.printText(sb.toString());
   }
}
